/**
 *  This file is part of AugeasWH.
 *
 *  AugeasWH. is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  AugeasWH. is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with AugeasWH.  If not, see <http://www.gnu.org/licenses/>.
 */

package cz.fit.vutbr.pis2012.augeaswh.control.whman.stock_items;

import java.io.Serializable;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import java.util.Locale;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cz.fit.vutbr.pis2012.augeaswh.managers.StockItemsManager;
import cz.fit.vutbr.pis2012.augeaswh.managers.StockTypesManager;
import cz.fit.vutbr.pis2012.augeaswh.managers.SupplierManager;
import cz.fit.vutbr.pis2012.augeaswh.persistence.SlotsStockItem;
import cz.fit.vutbr.pis2012.augeaswh.persistence.StockItem;
import cz.fit.vutbr.pis2012.augeaswh.persistence.StockType;
import cz.fit.vutbr.pis2012.augeaswh.persistence.Supplier;

@ManagedBean
@SessionScoped
public class StockItemsEditorBean implements Serializable {

	private static final long serialVersionUID = 1L;
	private StockItem selectedItem;
	private Supplier supplier;
	private StockType stockType;
	private Timestamp deliveryDate;
	private Locale locale;
	private String calendarPattern;
	
	@EJB
	private StockItemsManager sim;
	@EJB
	private SupplierManager sm;
	@EJB
	private StockTypesManager stm;
	
	public StockItemsEditorBean() {
		selectedItem = null;
		locale = Locale.GERMANY;
		calendarPattern = "MMM d, yyyy";
	}

	public StockItem getSelectedItem() {
		return selectedItem;
	}

	public void setSelectedItem(StockItem selectedItem) {
		selectedItem = (selectedItem == null) ? new StockItem() : selectedItem;
		if (selectedItem.getDeliveryDate() == null) 
			selectedItem.setDeliveryDate(new Timestamp(new Date().getTime()));
		
		supplier = (Supplier) selectedItem.getSupplier();
		stockType = selectedItem.getStockType();
		deliveryDate = selectedItem.getDeliveryDate();
		
		this.selectedItem = selectedItem;
	}
	
	public String save() {
		if (selectedItem != null) {
			selectedItem.setSupplier((Supplier) supplier);
			selectedItem.setStockType(stockType);
			selectedItem.setDeliveryDate(deliveryDate);
			sim.saveOrUpdateStockItem(selectedItem);
		}
		selectedItem = null;
		return "stock_items_list";
	}
	
	public String cancel() {
		selectedItem = null;
		return "stock_items_list";
	}
	
	public List<Supplier> getSuppliers() {
		return sm.getSuppliers();
	}
	
	public List<StockType> getStockTypes() {
		return stm.getStockTypes();
		//selectedItem.getSlotsStockItems();
	}
	
	public String getTitle() {
		if (selectedItem == null || selectedItem.getId() == null)
			return "Fill in form to add new stock item";
		return "Fill in form to edit stock item";
	}

	public Supplier getSupplier() {
		return supplier;
	}

	public void setSupplier(Supplier supplier) {
		this.supplier = supplier;
	}

	public StockType getStockType() {
		return stockType;
	}

	public void setStockType(StockType stockType) {
		this.stockType = stockType;
	}

	public Date getDeliveryDate() {
		return new Date(deliveryDate.getTime());
	}

	public void setDeliveryDate(Date deliveryDate) {
		this.deliveryDate = new Timestamp(deliveryDate.getTime());
	}

	public Locale getCalendarLocale() {
		return locale;
	}

	public void setCalendarLocale(Locale locale) {
		this.locale = locale;
	}

	public String getCalendarPattern() {
		return calendarPattern;
	}

	public void setCalendarPattern(String calendarPattern) {
		this.calendarPattern = calendarPattern;
	}
	
	public List<SlotsStockItem> getSlotStockItems() {
		List<SlotsStockItem> stockItems = (selectedItem == null) ? null : selectedItem.getSlotsStockItems();
		if (stockItems == null)
			stockItems = new ArrayList<SlotsStockItem>();
		return stockItems;
	}
}
